﻿using System.Windows.Forms;
using ComputerVision.BaseForm;
using ComputerVision.Data;
using ComputerVision.Services;
using Autofac;
using ComputerVision.Data.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;

namespace ComputerVision.LoaiCoSoForm
{
    public partial class frmBaoCaoTinhHinhChayNo : frmList
    {
        private IService<PhieuDieuTraCoSo> _phieuDieuTraCoSo;
        private readonly IService<LoaiCoSo> _loaiCoSo;
        private readonly IService<Nganh> _nganh;
        private readonly IService<QuanHuyen> _quanHuyen;
        private readonly IService<HinhThucDauTu> _hinhThucDauTu;
        private readonly IService<ChuyenDe> _chuyenDe;
        private readonly IService<NguyenNhanChay> _nguyenNhan;

        public frmBaoCaoTinhHinhChayNo()
        {
            InitializeComponent();

            report.AutoGenerateColumns = false;
            report.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;

            report.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            report.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;

            report.Columns[3].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            report.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;

            report.Columns[4].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            report.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;

            report.Columns[5].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            report.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;

            report.Columns[6].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            report.Columns[6].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;


            report.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

            _phieuDieuTraCoSo = Program.Container.Resolve<IService<PhieuDieuTraCoSo>>();
            _loaiCoSo = Program.Container.Resolve<IService<LoaiCoSo>>();
            _nganh = Program.Container.Resolve<IService<Nganh>>();
            _quanHuyen = Program.Container.Resolve<IService<QuanHuyen>>();
            _hinhThucDauTu = Program.Container.Resolve<IService<HinhThucDauTu>>();
            _chuyenDe = Program.Container.Resolve<IService<ChuyenDe>>();
            _nguyenNhan = Program.Container.Resolve<IService<NguyenNhanChay>>();

            LoadQuanHuyen();
            LoadHinhThucDauTu();
            LoadLoaiCoSo();
            LoadNganh();
            LoadNguyenNhan();
            LoadChuyenDe();
        }

        private void GenerateReport(object sender, System.EventArgs e)
        {
            progressBar.Value = 0;
            progressBar.Maximum = 0;
            BuildReportData();            
        }

        public void LoadLoaiCoSo()
        {
            var loaiCoSo = _loaiCoSo.ToList().OrderBy(c => c.Ten).ToList();
            loaiCoSo.Insert(0, new LoaiCoSo());
            drlLoaiCoSo.DataSource = loaiCoSo;
            drlLoaiCoSo.DisplayMember = "Ten";
            drlLoaiCoSo.ValueMember = "Id";
        }

        public void LoadHinhThucDauTu()
        {
            var hinhThucDauTu = _hinhThucDauTu.ToList().OrderBy(c => c.Ten).ToList();
            hinhThucDauTu.Insert(0, new HinhThucDauTu());
            drlHinhThucDauTu.DataSource = hinhThucDauTu;
            drlHinhThucDauTu.DisplayMember = "Ten";
            drlHinhThucDauTu.ValueMember = "Id";
        }

        public void LoadQuanHuyen()
        {
            var quanHuyen = _quanHuyen.ToList().OrderBy(q => q.Ten).ToList();
            var toanTinh = new QuanHuyen();
            toanTinh.Ten = "Toàn tỉnh";
            quanHuyen.Insert(0, toanTinh);
            drlQuanHuyen.DataSource = quanHuyen;
            drlQuanHuyen.DisplayMember = "Ten";
            drlQuanHuyen.ValueMember = "Id";
        }

        public void LoadNganh()
        {
            var nganh = _nganh.ToList().OrderBy(n => n.Ten).ToList();
            nganh.Insert(0, new Nganh());
            drlNganhNghe.DataSource = nganh;
            drlNganhNghe.DisplayMember = "Ten";
            drlNganhNghe.ValueMember = "Id";
        }

        public void LoadNguyenNhan()
        {
            var nguyenNhan = _nguyenNhan.ToList().OrderBy(n => n.NguyenNhan).ToList();
            nguyenNhan.Insert(0, new NguyenNhanChay());
            drlNguyenNhan.DataSource = nguyenNhan;
            drlNguyenNhan.DisplayMember = "NguyenNhan";
            drlNguyenNhan.ValueMember = "Id";
        }

        public void LoadChuyenDe()
        {
            var chuyenDe = _chuyenDe.ToList().OrderBy(h => h.Ten).ToList();
            chuyenDe.Insert(0, new ChuyenDe());
            drlChuyenDe.DataSource = chuyenDe;
            drlChuyenDe.DisplayMember = "Ten";
            drlChuyenDe.ValueMember = "Id";
        }

        private void BuildReportData()
        {
            var results = GetData();
            if (results.Count > 0)
            {
                string template = "- Ngày giờ xảy ra cháy: {1}{0}.Nơi phát sinh: {2}{0}.Nguyên nhân: {3}{0}";

                var data = new List<ReportThongKeCoSo>();                
                
                foreach (var item in results)
                {
                    string tenCoSo = Convert.ToString(item.TenCoSo);

                    var tempItem = data.Find(x => x.TenCoSo == tenCoSo);
                    if (tempItem == null)
                    {
                        var newItem = new ReportThongKeCoSo(item);
                        newItem.SoVuChay = 1;
                        newItem.DanhSachVuChay = string.Format(template,
                            Environment.NewLine,
                            item.Ngay.ToString("dd/MM/yyyy hh:mm"),
                            item.NoiPhatSinh,
                            item.NguyenNhanChay);
                        data.Add(newItem);
                    }
                    else
                    {
                        tempItem.SoVuChay++;
                        tempItem.SoNguoiChet += item.SoNguoiChet;
                        tempItem.SoNguoiBiThuong += item.SoNguoiBiThuong;
                        tempItem.ThietHaiTaiSan += item.ThietHaiTaiSan;

                        string vuchay = string.Format(template,
                            Environment.NewLine,
                            item.Ngay.ToString("dd/MM/yyyy hh:mm"),
                            item.NoiPhatSinh,
                            item.NguyenNhanChay);

                        tempItem.DanhSachVuChay = string.Concat(tempItem.DanhSachVuChay, vuchay);                        
                    }
                }

                report.DataSource = data;
            }
            else
            {
                report.Rows.Clear();
                report.DataSource = null;
            }
        }

        private List<ReportThongKeCoSo> GetData()
        {
            var quan = drlQuanHuyen.SelectedItem as QuanHuyen;
            var loaiCoSo = drlLoaiCoSo.SelectedItem as LoaiCoSo;
            var nganh = drlNganhNghe.SelectedItem as Nganh;
            var chuyenDe = drlChuyenDe.SelectedItem as ChuyenDe;
            var hinhThucDauTu = drlHinhThucDauTu.SelectedItem as HinhThucDauTu;
            var nguyenNhan = drlNguyenNhan.SelectedItem as NguyenNhanChay;

            var quanParam = new SqlParameter { ParameterName = "@Quan", Value = SqlGuid.Null, DbType = System.Data.DbType.Guid };
            var loaiCoSoParam = new SqlParameter { ParameterName = "@LoaiCoSo", Value = SqlGuid.Null, DbType = System.Data.DbType.Guid };
            var hinhThucParam = new SqlParameter { ParameterName = "@HinhThucDauTu", Value = SqlGuid.Null, DbType = System.Data.DbType.Guid };
            var nganhParam = new SqlParameter { ParameterName = "@Nganh", Value = SqlGuid.Null, DbType = System.Data.DbType.Guid };
            var chuyenDeParam = new SqlParameter { ParameterName = "@ChuyenDe", Value = SqlGuid.Null, DbType = System.Data.DbType.Guid };
            var nguyenNhanParam = new SqlParameter { ParameterName = "@NguyenNhan", Value = SqlGuid.Null, DbType = System.Data.DbType.Guid };

            List<PhieuDieuTraCoSo> results = new List<PhieuDieuTraCoSo>();

            if (!string.Equals(quan.Ten, "Toàn tỉnh", StringComparison.InvariantCultureIgnoreCase))            
            {
                quanParam = new SqlParameter { ParameterName = "@Quan", Value = quan.Id, DbType = System.Data.DbType.Guid };
            }

            if (!string.IsNullOrEmpty(loaiCoSo.Ten))
            {
                loaiCoSoParam = new SqlParameter { ParameterName = "@LoaiCoSo", Value = loaiCoSo.Id, DbType = System.Data.DbType.Guid };
            }

            if (!string.IsNullOrEmpty(hinhThucDauTu.Ten))
            {
                hinhThucParam = new SqlParameter { ParameterName = "@HinhThucDauTu", Value = hinhThucDauTu.Id, DbType = System.Data.DbType.Guid };
            }

            if (!string.IsNullOrEmpty(nganh.Ten))
            {
                nganhParam = new SqlParameter { ParameterName = "@Nganh", Value = nganh.Id, DbType = System.Data.DbType.Guid };
            }

            if (!string.IsNullOrEmpty(chuyenDe.Ten))
            {
                chuyenDeParam = new SqlParameter { ParameterName = "@ChuyenDe", Value = chuyenDe.Id, DbType = System.Data.DbType.Guid };
            }

            if (!string.IsNullOrEmpty(nguyenNhan.NguyenNhan))
            {
                nguyenNhanParam = new SqlParameter { ParameterName = "@NguyenNhan", Value = nguyenNhan.Id, DbType = System.Data.DbType.Guid };
            }

            QuanLyThongTinCoSoContext context = new QuanLyThongTinCoSoContext();
            return context.Database.SqlQuery<ReportThongKeCoSo>("Pro1 @Quan, @LoaiCoSo, @HinhThucDauTu, @Nganh, @ChuyenDe, @NguyenNhan",
                quanParam, loaiCoSoParam, hinhThucParam, nganhParam, chuyenDeParam, nguyenNhanParam).ToList();            
        }
    }
}
